﻿using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 清除公式的前景色、背景色、边框色并将边框宽度设置为１。
        /// </summary>
        /// <param name="withBorder">是否连边框一起清除。</param>
        public static void ExEraseAllColors(bool withBorder = true)
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            ModifingItem mi = new ModifingItem();

            Action actFore = new Action(curEx.ExID, -1, curEx.GetType().Name, "ExForeground",
                ColorPicker.GetBrushName(curEx.ExForeground),
                ColorPicker.GetBrushName(curEx.Editor.DefaultForeground));

            //2011年3月13日之后要考虑１－４级标题文本块的默认前景色。
            Expressions.BaseText bt = curEx as Expressions.BaseText;
            if (bt == null)
            {
                curEx.ExForeground = curEx.Editor.DefaultForeground;
                curEx.XmlData.RemoveAttributeByName("ExForeground");
            }
            else
            {
                curEx.XmlData.RemoveAttributeByName("ExForeground");
                bt.RefreshExForeground();
            }

            Action actBack = new Action(curEx.ExID, -1, curEx.GetType().Name, "ExBackground",
                ColorPicker.GetBrushName(curEx.ExBackground),
                ColorPicker.GetBrushName(Brushes.Transparent));
            curEx.ExBackground = Brushes.Transparent;
            curEx.XmlData.RemoveAttributeByName("ExBackground");

            mi.Add(actFore); mi.Add(actBack);

            if (withBorder)
            {
                Action actBorder = new Action(curEx.ExID, -1, curEx.GetType().Name, "BorderColor",
                    ColorPicker.GetBrushName(curEx.BorderColor),
                    ColorPicker.GetBrushName(Brushes.Transparent));
                curEx.BorderColor = Brushes.Transparent;
                curEx.XmlData.RemoveAttributeByName("BorderColor");

                if (curEx is Expressions.Grid || curEx is Expressions.GridPanel)
                {
                    if (curEx.Editor != null)
                        curEx.MainBorder.BorderBrush = curEx.Editor.DefaultForeground;
                }

                Action actBorderWidth = new Action(curEx.ExID, -1, curEx.GetType().Name, "BorderWidth",
                    curEx.BorderWidth.ToString(), 1.ToString());
                curEx.BorderWidth = 1;
                curEx.XmlData.RemoveAttributeByName("BorderWidth");

                mi.Add(actBorder); mi.Add(actBorderWidth);
            }
            curEx.Editor.Do(mi);
        }
    }
}
